package com.thrd.taiexhibition;

import android.content.Context;
import android.graphics.PixelFormat;
import android.graphics.Point;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;

/**
 * 底部广告悬浮
 */
public class AdvertisingWindow {
    private Context context;
    private View view;
    private WindowManager.LayoutParams mParams = null;
    private WindowManager windowManager = null;
    private static AdvertisingWindow floatMessagerMainWindow;

    public AdvertisingWindow(Context context, View view) {
        this.context = context;
        this.view = view;
      //  showWindow(context);
    }

    public static AdvertisingWindow getAdvertisingWindow(Context context, int view) {
        if (floatMessagerMainWindow == null) {
            synchronized (AdvertisingWindow.class) {
                if (floatMessagerMainWindow == null) {
                    floatMessagerMainWindow = new AdvertisingWindow(context, View.inflate(context,view,null));
                }
            }
        }
        return floatMessagerMainWindow;
    }

    public void showWindow() {
//  if (!isWindowDismiss) {
//   Log.e(TAG, "view is already added here");
//   return;
//  }
//  isWindowDismiss = false;
        if (windowManager == null) {
            windowManager = (WindowManager) context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
        }

        Point size = new Point();
        windowManager.getDefaultDisplay().getSize(size);
        int screenWidth = size.x;
        int screenHeight = size.y;

        mParams = new WindowManager.LayoutParams();
        mParams.packageName = context.getPackageName();
        mParams.width = screenWidth;
        mParams.height = dp2px(context,100);
        mParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
                | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
        mParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
//  mParams.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE |
//    WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN;
        mParams.format = PixelFormat.RGBA_8888;
        mParams.gravity = Gravity.LEFT | Gravity.BOTTOM;
//        mParams.x = screenWidth - 100;
//        mParams.y = 0;


//        ImageView imageView = new ImageView(context);
//        imageView.setImageResource(R.mipmap.logo);
//        imageView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                ShellCmd.getInstance(context).skateboardHangUp();
////                Toast.makeText(context, "image=========", Toast.LENGTH_SHORT).show();
////                View view = LayoutInflater.from(context).inflate(R.layout.float_pople_room_layout, null);
////                FloatMessagePopleDialog.getInstance(context, R.style.webviewTheme).setContextView(view);
//            }
//        });



//  floatView = new AVCallFloatView(context);
//  floatView.setParams(mParams);
//  floatView.setIsShowing(true);
//        View view=View.inflate(context,R.layout.popup_ad_bottom,null);

        windowManager.addView(view, mParams);
    }

    public void removeView(){
        if(windowManager!=null){
            windowManager.removeView(view);
        }
    }

    private int dp2px(Context context, float dp) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dp * scale + 0.1f);
    }
}
